import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// import './assets/styles/icon-font.css';
import "@/assets/font/iconfont";
import "./assets/styles/el-reset.css"
import qfSubMenu from "qf-sub-menu" //引入我们的组件
// Vue.config.productionTip = false;
//进度条
import nprogress from "nprogress"
import 'nprogress/nprogress.css'
Vue.use(qfSubMenu)


//路由守卫
router.beforeEach((to, from, next) => {
  //把进度条打开
  nprogress.start()
  let token = localStorage.getItem("myx-token");
  if (token) {
    if (store.state.userMenu.length === 0) {
      store.dispatch("FETCH_MENULIST")
        .then(() => {
          next({ ...to, replace: true })
        })
    } else {
      next()
    }

  } else {
    //没有登录不能访问其他页面

    //访问登入页面需要放行
    if (to.path === "/login") {
      next()
    } else {
      next({
        path: "/login"
      })
    }

  }
})

//全局后置钩子 afterEach中停止进度条

router.afterEach((to,from)=>{ //已经进入页面了
  
  //停止进度条
  nprogress.done();
// console.log(to)
// console.log(from)
  let matched=[...to.matched]

  matched.splice(0,1)
  store.commit('SET_BREADCRUMB', matched)
})


import "./assets/styles/icon-font.css"
Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");